/**
 * Package	: com.adams.custom.page.util
 * File		: ExcelUtil.java
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Create Date	: 2010-9-29
 * Create By    : 904941
 * History :$
 */
package com.adams.custom.page.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.map.LinkedMap;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/** 
 * 
 * File Name    : ExcelUtil.java
 * @Description 	: <Detail Description for Class >
 */
public class ExcelUtil {
	
	/**
	 * 
	 * Description 	: 生成临时文件名字
	 * 生成规则：~ Date().getTime() +"10位大小写"
	 * @return 临时文件名
	 *
	 */
	private static String createExcelFileName(){
		long date = new Date().getTime();
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		sb.append("~tmp");
		sb.append(date);
		sb.append("_");
		for(int i=0;i<10;i++){
			int rnum = r.nextInt(3);
			if(rnum == 0)
				sb.append((char)(r.nextInt(26)+65));
			else if(rnum==1)
				sb.append((char)(r.nextInt(26)+97));
			else
				sb.append(r.nextInt(10));
		}
		return sb.toString();
	}
	
	private static File writeDatasToTmpExcelFile(List recordList){
		HSSFWorkbook _workbook = null;
		HSSFSheet _sheet = null; 
		String tmpXlsFileName=createExcelFileName();
		File excelFile = new File(tmpXlsFileName);
		OutputStream excelOutput = null;
		try {
			
			// 创建临时文件
			excelFile.createTempFile("tmp", "xls");
			
			// 创建对象	
			_workbook = new HSSFWorkbook();
			excelOutput = new FileOutputStream(excelFile);
			
			// 创建sheet页对象
			_sheet = _workbook.createSheet("sheet1"); 
			
			if (null != recordList && recordList.size() > 0) {
				// 创建行对象
				for (int i = 0; i < recordList.size(); i++) {
					HSSFRow row = _sheet.createRow(i);
					LinkedMap dataList = (LinkedMap) recordList.get(i);
					for (int j = 0; j < dataList.size(); j++) {
						HSSFCell cell = row.createCell(j);
						cell.setCellValue(dataList.getValue(j) == null ? ""
								: dataList.getValue(j).toString());
					}
				}
			}
			_workbook.write(excelOutput);
			excelOutput.flush();
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}finally{
			if(excelOutput!=null){
				try {
					excelOutput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return excelFile;
	}
	
	/**
	 * 
	 * Description 	: 生成临时文件 返回临时文件输入流
	 * 用完之后请把流 关闭
	 * @param recordList 填充excel文件的数据记录
	 * @return 临时文件的输入流
	 *
	 */
	public static InputStream createExcelStream(List recordList) {
		try {
			File excelFile = ExcelUtil.writeDatasToTmpExcelFile(recordList);
			// 设置用完后删除临时文件
			excelFile.deleteOnExit();
			return new FileInputStream(excelFile);
		} catch (FileNotFoundException e) {
			return null;
		}
	}
	
	public static void main(String[] args) {
		List recodeList= new ArrayList();
		List dataList= new ArrayList();
		for(int i=0;i<5;i++){
			String str = String.valueOf(i);
			dataList.add(str+str+str);
		}
		for(int i=0;i<5;i++){
			recodeList.add(dataList);
		}
		writeDatasToTmpExcelFile(recodeList);
	}
}
